
package com.hlkj.pay.app.merchant.ext.impl.lakala.handler;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.http.template.OkHttpTemplate;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantAccessAuthQueryContext;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantBusinessContext;
import com.hlkj.pay.app.merchant.ext.impl.lakala.constant.LakalaConstant;
import com.hlkj.pay.app.merchant.ext.impl.lakala.dto.LakalaAccessExtentDto;
import com.hlkj.pay.app.merchant.ext.impl.lakala.dto.LakalaConfig;
import com.hlkj.pay.app.merchant.ext.impl.lakala.dto.req.*;
import com.hlkj.pay.app.merchant.ext.impl.lakala.dto.resp.LakalaAccessRes;
import com.hlkj.pay.app.merchant.ext.impl.lakala.dto.resp.LakalaSubMerInfoResp;
import com.hlkj.pay.app.merchant.ext.impl.lakala.dto.resp.LakalaSubMerInfoRespData;
import com.hlkj.pay.app.merchant.ext.impl.lakala.dto.resp.LakalaWechatConfigRes;
import com.hlkj.pay.app.merchant.ext.impl.lakala.utils.LKLSDK;
import com.hlkj.pay.app.merchant.ext.impl.lakala.utils.LakalaUtil;
import com.hlkj.pay.dto.merchant.MerchantWechatConfigAppIdDto;
import com.hlkj.pay.dto.merchant.MerchantWechatConfigDto;
import com.hlkj.pay.dto.merchant.MerchantWechatConfigListDto;
import com.hlkj.pay.dto.pay.PayCommonAppDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessAuthDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessInfoDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelRequestDO;
import com.lkl.laop.sdk.exception.SDKException;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/16 9:10
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Component
public class LakalaMerchantAccessConfigHandler extends LakalaMerchantCommonParamsHandle {

    private final OkHttpTemplate okHttpTemplate;

    /**
     * 微信参数配置查询
     *
     * @param channelMerchantBusinessContext
     */
    public void processSubMerInfoQuery(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<LakalaConfig> lakalaConfigCommonResult = lakalaConfig(channelMerchantBusinessContext.getProviderChannelConfig(), channelMerchantBusinessContext.isTest());
        // 配置转换
        if (!lakalaConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(lakalaConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        LakalaConfig lakalaConfig = lakalaConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
        try {
            List<MerchantAccessAuthDO> merchantAccessAuthList = new ArrayList<>();
            String openapiDomain = openapiDomain(channelMerchantBusinessContext.isTest());
            LKLSDK.init(lakalaConfig, openapiDomain);
            LakalaQuerySubMerInfoReq lakalaQuerySubMerInfoReq = new LakalaQuerySubMerInfoReq();
            lakalaQuerySubMerInfoReq.setOrgCode(lakalaConfig.getOrgCode());
            LakalaAccessExtentDto lakalaAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), LakalaAccessExtentDto.class);
            lakalaQuerySubMerInfoReq.setMerCupNo(lakalaAccessExtentDto.getMerCupNo());
            LakalaAccessReq lakalaAccessReq = new LakalaAccessReq();
            lakalaQuerySubMerInfoReq.setOrderNo(LakalaUtil.orderNo());
            lakalaAccessReq.setReqData(lakalaQuerySubMerInfoReq);
            String body = JsonUtils.toJsonString(lakalaAccessReq);

            log.info("拉卡拉商户报备信息查询请求参数:{}", body);
            try {
                String response = LKLSDK.httpPost(openapiDomain + LakalaConstant.MERCHANT_QUERY_SUB_MER_INFO, body, lakalaConfig.getAppId());
                log.info("拉卡拉商户报备信息查询req参数:{}", response);
                LakalaAccessRes<LakalaSubMerInfoRespData> lakalaRes = JsonUtils.parseObject(response, new TypeReference<LakalaAccessRes<LakalaSubMerInfoRespData>>() {
                });
                if (!LakalaConstant.MERCHANT_SUCCESS_CODE.equals(lakalaRes.getRetCode())) {
                    channelMerchantBusinessContext.setProcessStatus(false);
                    channelMerchantBusinessContext.setRemark(lakalaRes.getRetMsg());
                    return;
                }
                List<LakalaSubMerInfoResp> list = lakalaRes.getRespData().getList();
                for (LakalaSubMerInfoResp lakalaSubMerInfoResp : list) {
                    if (!StringUtils.hasText(lakalaSubMerInfoResp.getChannelId())) {
                        continue;
                    }
                    MerchantAccessAuthDO merchantAccessAuthDO = new MerchantAccessAuthDO();
                    // ZFBZF：支付宝
                    // WXZF：微信
                    // SNZF：苏宁钱包
                    // YZF：翼支付
                    // SZHB：数字货币
                    // NUCC：互联互通
                    // UNION：银联二维码
                    switch (lakalaSubMerInfoResp.getRegisterType()) {
                        case "ZFBZF":
                            merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.ALIPAY.getCode());
                            break;
                        case "WXZF":
                            merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.WECHAT.getCode());
                            break;
                        case "UNION":
                            merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.UNION_APY.getCode());
                            break;
                        default:
                            continue;
                    }
                    merchantAccessAuthDO.setAuthUrl("");
                    merchantAccessAuthDO.setAuthCode("");
                    if ("FAIL".equals(lakalaSubMerInfoResp.getRegisterStatus())) {
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.REJECT.getCode());
                    }
                    else {
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVEING.getCode());
                    }
                    merchantAccessAuthDO.setRejectParam(lakalaSubMerInfoResp.getRejectParameter());
                    if (StringUtils.hasText(lakalaSubMerInfoResp.getRejectReason())) {
                        merchantAccessAuthDO.setRejectReason(lakalaSubMerInfoResp.getRejectReason());
                    }
                    else {
                        merchantAccessAuthDO.setRejectReason(lakalaSubMerInfoResp.getResultMessage());
                    }
                    merchantAccessAuthDO.setMchId(lakalaSubMerInfoResp.getChannelId());
                    merchantAccessAuthDO.setSubMchId(lakalaSubMerInfoResp.getSubMchId());
                    merchantAccessAuthList.add(merchantAccessAuthDO);
                }
                lakalaAccessExtentDto.setMerInnerNo(list.get(0).getMerInnerNo());
                merchantAccessInfo.setExtendA(JsonUtils.toJsonString(lakalaAccessExtentDto));
                channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.WECHAT_CONFIG);
                channelMerchantBusinessContext.setMerchantAccessAuthList(merchantAccessAuthList);
                channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
            }
            catch (SDKException e) {
                channelMerchantBusinessContext.setProcessStatus(false);
                channelMerchantBusinessContext.setRemark(e.getMessage());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("拉卡拉商户报备信息查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantBusinessContext.setRemark("拉卡拉商户报备信息查询请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            //
            channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 微信参数配置
     * 
     * @param channelMerchantBusinessContext
     */
    public void processWxConfigSetInit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<LakalaConfig> lakalaConfigCommonResult = lakalaConfig(channelMerchantBusinessContext.getProviderChannelConfig(), channelMerchantBusinessContext.isTest());
        // 配置转换
        if (!lakalaConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(lakalaConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        LakalaConfig lakalaConfig = lakalaConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
        try {
            List<PayCommonAppDto> payCommonAppList = channelMerchantBusinessContext.getPayCommonAppList();
            List<PayCommonAppDto> wechatCommonApp = null;
            if (!CollectionUtils.isEmpty(payCommonAppList)) {
                wechatCommonApp = payCommonAppList.stream().filter(payCommonAppDto -> PayEnums.PAY_TYPE.WECHAT.getCode().equals(payCommonAppDto.getPayWayDO().getType()))
                        .collect(Collectors.toList());
            }
            // 没有微信执行 配置完成
            if (CollectionUtils.isEmpty(wechatCommonApp)) {
                channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
                return;
            }
            List<MerchantAccessAuthDO> merchantAccessAuthList = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessAuthList().stream()
                    .filter(merchantAccessAuthDO -> PayEnums.PAY_TYPE.WECHAT.getCode().equals(merchantAccessAuthDO.getType())).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(merchantAccessAuthList)) {
                channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
                return;
            }
            String openapiDomain = openapiDomain(channelMerchantBusinessContext.isTest());
            LKLSDK.init(lakalaConfig, openapiDomain);
            LakalaAccessExtentDto lakalaAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), LakalaAccessExtentDto.class);
            for (MerchantAccessAuthDO merchantAccessAuthDO : merchantAccessAuthList) {
                for (PayCommonAppDto payCommonAppDto : payCommonAppList) {
                    if (!StringUtils.hasText(merchantAccessAuthDO.getSubMchId())) {
                        continue;
                    }
                    LakalaWechatConfReq lakalaWechatConfReq = new LakalaWechatConfReq();
                    lakalaWechatConfReq.setMerCupNo(lakalaAccessExtentDto.getMerCupNo());
                    lakalaWechatConfReq.setChannlId(merchantAccessAuthDO.getMchId());
                    lakalaWechatConfReq.setSubMchId(merchantAccessAuthDO.getSubMchId());
                    // lakalaWechatConfReq.setSubscribeAppid();
                    lakalaWechatConfReq.setSubAppid(payCommonAppDto.getAppId());
                    // lakalaWechatConfReq.setJsapiPath();
                    // 配置类型 类型=SUB_APPID时，subAppid必传 类型=SUBSCRIBE_APPID时，subscribeAppid必传
                    // 类型=UNION时，subAppid和subscribeAppid必传 类型=JSAPI_PATH时，jsapiPath必传
                    lakalaWechatConfReq.setConfType("SUB_APPID");
                    LakalaAccessReq lakalaAccessReq = new LakalaAccessReq();
                    lakalaAccessReq.setReqData(lakalaWechatConfReq);
                    String body = JsonUtils.toJsonString(lakalaAccessReq);
                    log.info("拉卡拉微信配置init信息请求参数:{}", body);
                    try {
                        String response = LKLSDK.httpPost(openapiDomain + LakalaConstant.MERCHANT_WECHAT_CONFIG_SET, body, lakalaConfig.getAppId());
                        LakalaAccessRes lakalaRes = JsonUtils.parseObject(response, LakalaAccessRes.class);
                        log.info("拉卡拉微信配置init信息response参数:{}", JsonUtils.toJsonString(response));
                        if (!LakalaConstant.MERCHANT_SUCCESS_CODE.equals(lakalaRes.getRetCode())) {
                            channelMerchantBusinessContext.setProcessStatus(false);
                            channelMerchantBusinessContext.setRemark(lakalaRes.getRetMsg());
                            return;
                        }
                    }
                    catch (SDKException e) {
                        // throw new RuntimeException(e);
                        channelMerchantBusinessContext.setProcessStatus(false);
                        channelMerchantBusinessContext.setRemark(e.getMessage());
                        return;
                    }
                }
            }
            channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.WECHAT_AUTH);

        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("拉卡拉微信配置init请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantBusinessContext.setRemark("拉卡拉微信配置init请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    public void processWxConfigSetAdd(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<LakalaConfig> lakalaConfigCommonResult = lakalaConfig(channelMerchantBusinessContext.getProviderChannelConfig(), channelMerchantBusinessContext.isTest());
        // 配置转换
        if (!lakalaConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(lakalaConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        LakalaConfig lakalaConfig = lakalaConfigCommonResult.getData();
        // 1. 数据初始化
        PayChannelRequestDO payChannelRequestDO = new PayChannelRequestDO();
        channelMerchantBusinessContext.setPayChannelRequestDO(payChannelRequestDO);
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
        MerchantWechatConfigDto merchantWechatConfigDto = channelMerchantBusinessContext.getMerchantWechatConfigDto();
        ;
        try {
            // 没有微信执行 配置完成
            String openapiDomain = openapiDomain(channelMerchantBusinessContext.isTest());
            LKLSDK.init(lakalaConfig, openapiDomain);
            MerchantAccessAuthDO merchantAccessAuthDO = channelMerchantBusinessContext.getMerchantAccessAuth();
            if (!PayEnums.PAY_TYPE.WECHAT.getCode().equals(merchantAccessAuthDO.getType())) {
                log.info("拉卡拉微信配置 非报备非微信类型直接返回 type:{},subMchId:{}", merchantAccessAuthDO.getType(), merchantAccessAuthDO.getSubMchId());
                return;
            }
            LakalaAccessExtentDto lakalaAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), LakalaAccessExtentDto.class);
            if (StringUtils.hasText(merchantWechatConfigDto.getAppId())) {
                LakalaWechatConfReq lakalaWechatConfReq = new LakalaWechatConfReq();
                lakalaWechatConfReq.setMerCupNo(lakalaAccessExtentDto.getMerCupNo());
                lakalaWechatConfReq.setChannlId(merchantAccessAuthDO.getMchId());
                lakalaWechatConfReq.setSubMchId(merchantAccessAuthDO.getSubMchId());
                lakalaWechatConfReq.setSubAppid(merchantWechatConfigDto.getAppId());
                // lakalaWechatConfReq.setJsapiPath();
                // 配置类型 类型=SUB_APPID时，subAppid必传 类型=SUBSCRIBE_APPID时，subscribeAppid必传
                // 类型=UNION时，subAppid和subscribeAppid必传 类型=JSAPI_PATH时，jsapiPath必传
                lakalaWechatConfReq.setConfType("SUB_APPID");
                LakalaAccessReq lakalaAccessReq = new LakalaAccessReq();
                lakalaAccessReq.setReqData(lakalaWechatConfReq);
                String body = JsonUtils.toJsonString(lakalaAccessReq);
                log.info("拉卡拉微信配置add信息请求参数:{}", body);
                try {
                    String response = LKLSDK.httpPost(openapiDomain + LakalaConstant.MERCHANT_WECHAT_CONFIG_SET, body, lakalaConfig.getAppId());
                    LakalaAccessRes lakalaRes = JsonUtils.parseObject(response, LakalaAccessRes.class);
                    log.info("拉卡拉微信配置add信息response参数:{}", JsonUtils.toJsonString(response));
                    if (!LakalaConstant.MERCHANT_SUCCESS_CODE.equals(lakalaRes.getRetCode())) {
                        channelMerchantBusinessContext.setProcessStatus(false);
                        channelMerchantBusinessContext.setRemark(lakalaRes.getRetMsg());
                        return;
                    }
                }
                catch (SDKException e) {
                    // throw new RuntimeException(e);
                    channelMerchantBusinessContext.setProcessStatus(false);
                    channelMerchantBusinessContext.setRemark(e.getMessage());
                    return;
                }
            }
            if (StringUtils.hasText(merchantWechatConfigDto.getJsAPIPath())) {
                LakalaWechatConfReq lakalaWechatConfReq = new LakalaWechatConfReq();
                lakalaWechatConfReq.setMerCupNo(lakalaAccessExtentDto.getMerCupNo());
                lakalaWechatConfReq.setChannlId(merchantAccessAuthDO.getMchId());
                lakalaWechatConfReq.setSubMchId(merchantAccessAuthDO.getSubMchId());
                // lakalaWechatConfReq.setSubscribeAppid();
                lakalaWechatConfReq.setJsapiPath(merchantWechatConfigDto.getJsAPIPath());
                // lakalaWechatConfReq.setJsapiPath();
                // 配置类型 类型=SUB_APPID时，subAppid必传 类型=SUBSCRIBE_APPID时，subscribeAppid必传
                // 类型=UNION时，subAppid和subscribeAppid必传 类型=JSAPI_PATH时，jsapiPath必传
                lakalaWechatConfReq.setConfType("JSAPI_PATH");
                LakalaAccessReq lakalaAccessReq = new LakalaAccessReq();
                lakalaAccessReq.setReqData(lakalaWechatConfReq);
                String body = JsonUtils.toJsonString(lakalaAccessReq);
                log.info("拉卡拉微信配置add信息请求参数:{}", body);
                try {
                    String response = LKLSDK.httpPost(openapiDomain + LakalaConstant.MERCHANT_WECHAT_CONFIG_SET, body, lakalaConfig.getAppId());
                    LakalaAccessRes lakalaRes = JsonUtils.parseObject(response, LakalaAccessRes.class);
                    log.info("拉卡拉微信配置add信息response参数:{}", JsonUtils.toJsonString(response));
                    if (!LakalaConstant.MERCHANT_SUCCESS_CODE.equals(lakalaRes.getRetCode())) {
                        channelMerchantBusinessContext.setProcessStatus(false);
                        channelMerchantBusinessContext.setRemark(lakalaRes.getRetMsg());
                        return;
                    }
                }
                catch (SDKException e) {
                    // throw new RuntimeException(e);
                    channelMerchantBusinessContext.setProcessStatus(false);
                    channelMerchantBusinessContext.setRemark(e.getMessage());
                    return;
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("拉卡拉微信配置add请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantBusinessContext.setRemark("拉卡拉微信配置add请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 微信参数配置查询
     * 
     * @param channelMerchantAccessAuthQueryContext
     */
    public void processWxConfigQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        CommonResult<LakalaConfig> lakalaConfigCommonResult = lakalaConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        // 配置转换
        if (!lakalaConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(lakalaConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        LakalaConfig lakalaConfig = lakalaConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        try {
            List<MerchantAccessAuthDO> merchantAccessAuthList = channelMerchantAccessAuthQueryContext.getMerchantAccessAuthList();
            // 没有微信报备 执行支付宝认证
            if (CollectionUtils.isEmpty(merchantAccessAuthList)) {
                channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
                return;
            }
            String openapiDomain = openapiDomain(channelMerchantAccessAuthQueryContext.isTest());
            LKLSDK.init(lakalaConfig, openapiDomain);
            MerchantWechatConfigListDto merchantWechatConfigListDto = new MerchantWechatConfigListDto();
            List<MerchantWechatConfigAppIdDto> appIdList = new ArrayList<>();
            List<String> jsApiPath = new ArrayList<>();
            for (MerchantAccessAuthDO merchantAccessAuthDO : merchantAccessAuthList) {
                if (!PayEnums.PAY_TYPE.WECHAT.getCode().equals(merchantAccessAuthDO.getType())) {
                    log.info("拉卡拉微信配置查询 非报备非微信类型直接返回 type:{},subMchId:{}", merchantAccessAuthDO.getType(), merchantAccessAuthDO.getSubMchId());
                    continue;
                }
                if (!StringUtils.hasText(merchantAccessAuthDO.getMchId())) {
                    log.info("拉卡拉微信配置查询 微信子商户号不存在 accessId:{},subMchId:{}", merchantAccessAuthDO.getAccessId(), merchantAccessAuthDO.getSubMchId());
                    continue;
                }
                LakalaAccessExtentDto lakalaAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), LakalaAccessExtentDto.class);
                LakalaWechatConfQueryReq lakalaWechatConfReq = new LakalaWechatConfQueryReq();
                lakalaWechatConfReq.setSubMerchantId(merchantAccessAuthDO.getMchId());
                lakalaWechatConfReq.setMerchantNo(lakalaAccessExtentDto.getMerCupNo());
                LakalaAccessReq lakalaAccessReq = new LakalaAccessReq();
                lakalaAccessReq.setReqData(lakalaWechatConfReq);
                String body = JsonUtils.toJsonString(lakalaAccessReq);
                log.info("拉卡拉微信配置查询信息请求参数:{}", body);
                try {
                    String response = LKLSDK.httpPost(openapiDomain + LakalaConstant.MERCHANT_WECHAT_CONFIG_QUERY, body, lakalaConfig.getAppId());
                    LakalaAccessRes<LakalaWechatConfigRes> lakalaRes = JsonUtils.parseObject(response, LakalaAccessRes.class);
                    log.info("拉卡拉微信配置查询信息response参数:{}", JsonUtils.toJsonString(response));
                    if (!LakalaConstant.MERCHANT_SUCCESS_CODE.equals(lakalaRes.getRetCode())) {
                        channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                        channelMerchantAccessAuthQueryContext.setRemark(lakalaRes.getRetMsg());
                        return;
                    }
                    LakalaWechatConfigRes respData = lakalaRes.getRespData();
                    if (respData != null) {
                        if (!CollectionUtils.isEmpty(respData.getAppidConfigList())) {
                            respData.getAppidConfigList().stream().forEach(resp -> {
                                MerchantWechatConfigAppIdDto merchantWechatConfigAppIdDto = new MerchantWechatConfigAppIdDto();
                                merchantWechatConfigAppIdDto.setSubAppid(resp.getSubAppid());
                                merchantWechatConfigAppIdDto.setSubscribeAppid(resp.getSubscribeAppid());
                                appIdList.add(merchantWechatConfigAppIdDto);
                            });
                        }
                        if (!CollectionUtils.isEmpty(respData.getJsapiPathList())) {
                            jsApiPath.addAll(respData.getJsapiPathList());
                        }
                    }
                }
                catch (SDKException e) {
                    e.printStackTrace();
                    channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                    channelMerchantAccessAuthQueryContext.setRemark(e.getMessage());
                }
            }
            merchantWechatConfigListDto.setAppIdList(appIdList);
            merchantWechatConfigListDto.setJsApiPath(jsApiPath);
            channelMerchantAccessAuthQueryContext.setMerchantWechatConfigListDto(merchantWechatConfigListDto);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("拉卡拉微信配置查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantAccessAuthQueryContext.setRemark("拉卡拉微信配置查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessAuthQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 微信参数配置查询
     *
     * @param channelMerchantAccessAuthQueryContext
     */
    public void processWxAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        CommonResult<LakalaConfig> lakalaConfigCommonResult = lakalaConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        // 配置转换
        if (!lakalaConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(lakalaConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        LakalaConfig lakalaConfig = lakalaConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        try {
            List<MerchantAccessAuthDO> merchantAccessAuthList = channelMerchantAccessAuthQueryContext.getMerchantAccessAuthDbList();
            channelMerchantAccessAuthQueryContext.setMerchantAccessAuthList(merchantAccessAuthList);
            // 没有微信报备 执行支付宝认证
            if (CollectionUtils.isEmpty(merchantAccessAuthList)) {
                channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
                return;
            }
            String openapiDomain = openapiDomain(channelMerchantAccessAuthQueryContext.isTest());
            LKLSDK.init(lakalaConfig, openapiDomain);
            LakalaAccessExtentDto lakalaAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), LakalaAccessExtentDto.class);
            for (MerchantAccessAuthDO merchantAccessAuthDO : merchantAccessAuthList) {
                if (!PayEnums.PAY_TYPE.WECHAT.getCode().equals(merchantAccessAuthDO.getType())) {
                    log.info("拉卡拉微信认证查询 非报备非微信类型直接返回 type:{},subMchId:{}", merchantAccessAuthDO.getType(), merchantAccessAuthDO.getSubMchId());
                    continue;
                }
                if (!StringUtils.hasText(merchantAccessAuthDO.getMchId())) {
                    log.info("拉卡拉微信认证查询 微信子商户号不存在 accessId:{},subMchId:{}", merchantAccessAuthDO.getAccessId(), merchantAccessAuthDO.getSubMchId());
                    continue;
                }
                LakalaAuthConfReq lakalaAuthConfReq = new LakalaAuthConfReq();
                lakalaAuthConfReq.setSubMchId(merchantAccessAuthDO.getSubMchId());
                lakalaAuthConfReq.setMerInnerNo(lakalaAccessExtentDto.getMerInnerNo());
                lakalaAuthConfReq.setOrgCode(lakalaConfig.getOrgCode());
                lakalaAuthConfReq.setOrderNo(LakalaUtil.orderNo());
                LakalaAccessReq lakalaAccessReq = new LakalaAccessReq();
                lakalaAccessReq.setReqData(lakalaAuthConfReq);
                String body = JsonUtils.toJsonString(lakalaAccessReq);
                log.info("拉卡拉微信认证查询信息请求参数:{}", body);
                try {
                    String response = LKLSDK.httpPost(openapiDomain + LakalaConstant.MERCHANT_WECHAT_AUTH_QUERY, body, lakalaConfig.getAppId());
                    LakalaAccessRes<LakalaSubMerInfoResp> lakalaRes = JsonUtils.parseObject(response, new TypeReference<LakalaAccessRes<LakalaSubMerInfoResp>>() {
                    });
                    log.info("拉卡拉微信认证查询信息response参数:{}", JsonUtils.toJsonString(response));
                    if (!LakalaConstant.MERCHANT_SUCCESS_CODE.equals(lakalaRes.getRetCode())) {
                        channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                        channelMerchantAccessAuthQueryContext.setRemark(lakalaRes.getRetMsg());
                        return;
                    }
                    LakalaSubMerInfoResp respData = lakalaRes.getRespData();
                    if (respData != null) {
                        // APPLYMENT_STATE_FAIL：提交失败；
                        // APPLYMENT_STATE_COMMIT：已提交；
                        // APPLYMENT_STATE_WAITTING_FOR_AUDIT：审核中；
                        // APPLYMENT_STATE_EDITTING：编辑中；
                        // APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT：待确认联系信息；
                        // APPLYMENT_STATE_WAITTING_FOR_CONFIRM_LEGALPERSON：待账户验证；
                        // APPLYMENT_STATE_PASSED：审核通过；
                        // APPLYMENT_STATE_REJECTED：审核驳回；
                        // APPLYMENT_STATE_FREEZED：已冻结；
                        // APPLYMENT_STATE_CANCELED：已作废。
                        switch (respData.getApplymentState()) {
                            case "APPLYMENT_STATE_FAIL":
                            case "APPLYMENT_STATE_REJECTED":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.REJECT.getCode());
                                break;
                            case "APPLYMENT_STATE_COMMIT":
                            case "APPLYMENT_STATE_WAITTING_FOR_AUDIT":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVEING.getCode());
                                break;
                            case "APPLYMENT_STATE_EDITTING":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.NO_APPLY.getCode());
                                break;
                            case "APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.WAIT_CONFIRM.getCode());
                                break;
                            case "APPLYMENT_STATE_WAITTING_FOR_CONFIRM_LEGALPERSON":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.WAIT_AUTH.getCode());
                                break;
                            case "APPLYMENT_STATE_PASSED":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVE.getCode());
                                break;
                            case "APPLYMENT_STATE_FREEZED":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.FROZEN.getCode());
                                break;
                            case "APPLYMENT_STATE_CANCELED":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.CANCEL.getCode());
                                break;
                            default:
                        }
                        switch (respData.getAuthorizeState()) {
                            case "AUTHORIZE_STATE_AUTHORIZED":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.AUTH_SUCCESS.getCode());
                                break;
                            default:
                        }
                        if (StringUtils.hasText(respData.getQrcodeData())) {
                            byte[] imageBytes = Base64.getDecoder().decode(respData.getQrcodeData());
                            merchantAccessAuthDO.setQrCodeArray(imageBytes);
                        }
                        merchantAccessAuthDO.setRejectReason(respData.getRejectReason());
                        merchantAccessAuthDO.setRejectParam(respData.getRejectParameter());
                    }
                }
                catch (SDKException e) {
                    e.printStackTrace();
                    channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                    channelMerchantAccessAuthQueryContext.setRemark(e.getMessage());
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("拉卡拉微信认证查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantAccessAuthQueryContext.setRemark("拉卡拉微信认证查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessAuthQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 微信参数配置查询
     *
     * @param channelMerchantAccessAuthQueryContext
     */
    public void processAliAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        CommonResult<LakalaConfig> lakalaConfigCommonResult = lakalaConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        // 配置转换
        if (!lakalaConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(lakalaConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        LakalaConfig lakalaConfig = lakalaConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        try {
            List<MerchantAccessAuthDO> merchantAccessAuthList = channelMerchantAccessAuthQueryContext.getMerchantAccessAuthDbList();
            channelMerchantAccessAuthQueryContext.setMerchantAccessAuthList(merchantAccessAuthList);
            // 没有微信报备 执行支付宝认证
            if (CollectionUtils.isEmpty(merchantAccessAuthList)) {
                channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
                return;
            }
            String openapiDomain = openapiDomain(channelMerchantAccessAuthQueryContext.isTest());
            LKLSDK.init(lakalaConfig, openapiDomain);
            LakalaAccessExtentDto lakalaAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), LakalaAccessExtentDto.class);
            for (MerchantAccessAuthDO merchantAccessAuthDO : merchantAccessAuthList) {
                if (!PayEnums.PAY_TYPE.ALIPAY.getCode().equals(merchantAccessAuthDO.getType())) {
                    log.info("拉卡拉支付宝认证查询 非报备非微信类型直接返回 type:{},subMchId:{}", merchantAccessAuthDO.getType(), merchantAccessAuthDO.getSubMchId());
                    continue;
                }
                if (!StringUtils.hasText(merchantAccessAuthDO.getMchId())) {
                    log.info("拉卡拉支付宝认证查询 支付宝子商户号不存在 accessId:{},subMchId:{}", merchantAccessAuthDO.getAccessId(), merchantAccessAuthDO.getSubMchId());
                    continue;
                }
                LakalaAuthConfReq lakalaAuthConfReq = new LakalaAuthConfReq();
                lakalaAuthConfReq.setSubMchId(merchantAccessAuthDO.getSubMchId());
                lakalaAuthConfReq.setMerInnerNo(lakalaAccessExtentDto.getMerInnerNo());
                lakalaAuthConfReq.setOrgCode(lakalaConfig.getOrgCode());
                lakalaAuthConfReq.setOrderNo(LakalaUtil.orderNo());
                LakalaAccessReq lakalaAccessReq = new LakalaAccessReq();
                lakalaAccessReq.setReqData(lakalaAuthConfReq);
                String body = JsonUtils.toJsonString(lakalaAccessReq);
                log.info("拉卡拉支付宝认证查询信息请求参数:{}", body);
                try {
                    String response = LKLSDK.httpPost(openapiDomain + LakalaConstant.MERCHANT_ALIPAY_AUTH_QUERY, body, lakalaConfig.getAppId());
                    LakalaAccessRes<LakalaSubMerInfoResp> lakalaRes = JsonUtils.parseObject(response, new TypeReference<LakalaAccessRes<LakalaSubMerInfoResp>>() {
                    });
                    log.info("拉卡拉支付宝认证查询信息response参数:{}", JsonUtils.toJsonString(response));
                    if (!LakalaConstant.MERCHANT_SUCCESS_CODE.equals(lakalaRes.getRetCode())) {
                        channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                        channelMerchantAccessAuthQueryContext.setRemark(lakalaRes.getRetMsg());
                        return;
                    }
                    LakalaSubMerInfoResp respData = lakalaRes.getRespData();
                    if (respData != null) {
                        // APPLYMENT_STATE_FAIL：提交失败；
                        // APPLYMENT_STATE_COMMIT：已提交；
                        // AUDITING：审核中；
                        // APPLYMENT_STATE_EDITTING：编辑中；
                        // CONTACT_CONFIRM：待确认联系信息；
                        // LEGAL_CONFIRM：待账户验证；
                        // AUDIT_PASS：审核通过；
                        // AUDIT_REJECT：审核驳回；
                        // AUDIT_FREEZE：已冻结；
                        // CANCELED：已撤回。
                        switch (respData.getApplymentState()) {
                            case "APPLYMENT_STATE_FAIL":
                            case "AUDIT_REJECT":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.REJECT.getCode());
                                break;
                            case "APPLYMENT_STATE_COMMIT":
                            case "AUDITING":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVEING.getCode());
                                break;
                            case "APPLYMENT_STATE_EDITTING":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.NO_APPLY.getCode());
                                break;
                            case "CONTACT_CONFIRM":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.WAIT_CONFIRM.getCode());
                                break;
                            case "LEGAL_CONFIRM":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.WAIT_AUTH.getCode());
                                break;
                            case "AUDIT_PASS":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVE.getCode());
                                break;
                            case "AUDIT_FREEZE":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.FROZEN.getCode());
                                break;
                            case "CANCELED":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.CANCEL.getCode());
                                break;
                            default:
                        }
                        switch (respData.getAuthorizeState()) {
                            case "AUTHORIZED":
                                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.AUTH_SUCCESS.getCode());
                                break;
                            default:
                        }
                        if (StringUtils.hasText(respData.getQrcodeData())) {
                            merchantAccessAuthDO.setAuthUrl(respData.getQrcodeData());
                        }
                        merchantAccessAuthDO.setRejectReason(respData.getRejectReason());
                        merchantAccessAuthDO.setRejectParam(respData.getRejectParameter());

                    }
                }
                catch (SDKException e) {
                    e.printStackTrace();
                    channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                    channelMerchantAccessAuthQueryContext.setRemark(e.getMessage());
                }
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("拉卡拉支付宝认证查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantAccessAuthQueryContext.setRemark("拉卡拉支付宝认证查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessAuthQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

}
